package com.example.xm.ui.home.activity.youhome_fragments;

import android.os.Bundle;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.YouHomeSocietyAdapter;
import com.example.xm.base.BaseFragment;
import com.example.xm.bean.HomeBean.HomeYouHomeSocietyBean;
import com.example.xm.interfaces.IBasePresenter;
import com.example.xm.interfaces.ILoadData;

import java.util.ArrayList;

import butterknife.BindView;

public class SocietyFragment extends BaseFragment {
    @BindView(R.id.rlv_society)
    RecyclerView rlvSociety;
    private HomeYouHomeSocietyBean data;
    private ArrayList<HomeYouHomeSocietyBean.DataBean> list;
    private YouHomeSocietyAdapter youHomeSocietyAdapter;
    private ILoadData loadData;
    private int type;

    public static SocietyFragment getFragment(int type, ILoadData loadData) {
        SocietyFragment societyFragment = new SocietyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        societyFragment.loadData = loadData;
        societyFragment.setArguments(bundle);
        return societyFragment;
    }

    @Override
    protected IBasePresenter initPresenter() {
        return null;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && rlvSociety != null) {
            if (data != null) {
                if (loadData != null){
//                    loadData.loadData(type);
                }
            }
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_youhome_society;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        rlvSociety.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlvSociety.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        youHomeSocietyAdapter = new YouHomeSocietyAdapter(getActivity(), list);
        rlvSociety.setAdapter(youHomeSocietyAdapter);
        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey("type")) {
            type = bundle.getInt("type");
        }
        if (loadData != null) {
            loadData.loadData(type);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    //设置数据
    public void getData(HomeYouHomeSocietyBean bean) {
        data = bean;
        list.addAll(bean.getData());
        upData();
    }

    //刷新数据
    public void upData() {
        youHomeSocietyAdapter.notifyDataSetChanged();
    }
}
